/*
 * @Author: wangdazhuang
 * @Date: 2024-10-11 09:21:13
 * @LastEditTime: 2025-01-08 10:39:46
 * @LastEditors: ziqi jhzq12345678
 * @Description: 
 * @FilePath: /pornhub_app/lib/src/views/player/views/av_change_line_pop.dart
 */

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:pornhub_app/assets/styles.dart';
import 'package:pornhub_app/components/grid_view/heighted_grid_view.dart';
import 'package:pornhub_app/utils/color.dart';
import 'package:pornhub_app/utils/extension.dart';
import 'package:pornhub_app/views/player/controllers/video_play_controller.dart';

class AvChangeLinePop extends StatelessWidget {
  const AvChangeLinePop({super.key});

  _buildTitle() {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Text(
              '网络不好？请尝试切换线路',
              style: kTextStyle(
                COLOR.hexColor("#fff"),
                fontsize: 14.w,
              ),
            ),
            Text(
              '网络加载慢，也可尝试切换线路，直至正常',
              style: kTextStyle(
                COLOR.hexColor("#A4A4B2"),
                fontsize: 12.w,
              ),
            )
          ],
        ),
        Icon(
          Icons.close,
          size: 30.w,
          color: COLOR.hexColor("#fff"),
        ).onTap(() {
          Get.back();
        }),
      ],
    ).marginBottom(20.w);
  }

  _buildLineList(VideoPlayController vc) {
    return HeightedGridView(
      rowMainAxisAlignment: MainAxisAlignment.start,
      rowCrossAxisAlignment: CrossAxisAlignment.start,
      crossAxisCount: 1,
      rowSepratorBuilder: (context, index) {
        return SizedBox(height: 15.w);
      },
      itemCount: vc.lines.length,
      itemBuilder: (context, index) {
        final item = vc.lines[index];
        var isSelected = item.id != null &&
            vc.selectCdn.id!.isNotEmpty &&
            item.id! == vc.selectCdn.id!;
        return Container(
          width: 360.w,
          height: 35.w,
          alignment: Alignment.center,
          decoration: BoxDecoration(
              color: isSelected
                  ? COLOR.hexColor("#1C1C1C")
                  : COLOR.hexColor("#1D1D1D"),
              border: Border.all(
                  color: COLOR.hexColor(isSelected ? '#B93FFF' : '#1C1C1C')),
              borderRadius: BorderRadius.circular(6.w)),
          child: Text(
            item.line!,
            overflow: TextOverflow.ellipsis,
            style: kTextStyle(
              Colors.white,
              fontsize: 12.w,
            ),
          ),
        ).onOpaqueTap(() {
          if (isSelected) {
            return;
          }
          vc.changeLine(item);
        });
      },
    );
  }

  Widget _buildSureBtn(VideoPlayController vc) {
    return SizedBox(
      width: 360.w,
      height: 60.w,
      child: Image.asset(
        'assets/player/sure_change.png',
      ).onOpaqueTap(() {
        vc.sureChangeLine();
        SmartDialog.showToast("切换成功!", alignment: Alignment.center);
        Get.back();
      }),
    );
  }

  @override
  Widget build(BuildContext context) {
    return GetBuilder<VideoPlayController>(
      builder: (controller) {
        return Container(
            width: Get.width,
            height: 344.w,
            padding: EdgeInsets.all(20.w),
            decoration: BoxDecoration(
                color: COLOR.hexColor('#111111'),
                borderRadius: BorderRadius.only(
                    topLeft: Radius.circular(
                      14.w,
                    ),
                    topRight: Radius.circular(14.w))),
            child: Flex(
              direction: Axis.vertical,
              children: [
                _buildTitle(),
                Expanded(child: _buildLineList(controller)),
                _buildSureBtn(controller)
              ],
            ));
      },
    );
  }
}
